﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BCCommon;
using System.Text;
using System.Net;
using Microsoft.AspNetCore.Mvc;
using BCDto.Trading.SaleContract;
using BCTMSRESTful.Configs;
using BCService.Utils;
using BCService.Trading.SaleContract;
using BCCommon.TradingEnums;

namespace BCTMSRESTful.Controllers.Management.Trading.SaleContract
{
    /// <summary>
    /// 销售合同
    /// </summary>
    [ApiExplorerSettings(GroupName = "Admin")]
    [Route("api/v1/sim/[controller]")]
    public class SaleContractController : ManagementBaseController
    {
        private readonly ISaleContractService saleContractService;

        public SaleContractController(ISaleContractService saleContractService)
        {
            this.saleContractService = saleContractService;
        }

        #region 添加销售合同
        /// <summary>
        ///  添加销售合同
        /// </summary>
        /// <param name="requestDto">销售合同信息</param>
        /// <response code="201">添加销售合同成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPost]
        [Log("添加销售合同")]
        [ProducesResponseType(typeof(SaleContractDto), 201)]
        public IActionResult Post(
            [FromBody]SaleContractRequestDto requestDto)
        {
            var result = this.saleContractService.Add(requestDto);
            return StatusCode((int)HttpStatusCode.Created, result);
        }
        #endregion

        #region 修改销售合同
        /// <summary>
        ///  修改销售合同
        /// </summary>
        /// <param name="saleContractId">销售合同Id</param>
        /// <param name="requestDto">销售合同信息</param>
        /// <response code="200">修改销售合同成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPut("{sale_contract_id}")]
        [Log("修改销售合同")]
        [ProducesResponseType(typeof(SaleContractDto), 200)]
        public IActionResult Put([FromRoute(Name = "sale_contract_id")]long saleContractId, [FromBody]SaleContractRequestDto requestDto)
        {
            SaleContractPutDto putDto = requestDto.As<SaleContractPutDto>();
            putDto.SaleContractId = saleContractId;
            return StatusCode((int)HttpStatusCode.OK, this.saleContractService.Update(putDto));
        }
        #endregion

        #region 删除销售合同
        /// <summary>
        ///  删除销售合同
        /// </summary>
        /// <param name="saleContractId">销售合同Id</param>
        /// <response code="204">删除销售合同成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpDelete("{sale_contract_id}")]
        [Log("删除销售合同")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult Delete([FromRoute(Name = "sale_contract_id")]long saleContractId)
        {
            this.saleContractService.Delete(saleContractId);
            return StatusCode((int)HttpStatusCode.NoContent);
        }
        #endregion

        #region 获取销售合同
        /// <summary>
        ///  获取销售合同
        /// </summary>
        /// <param name="saleContractId">销售合同Id</param>
        /// <response code="200">获取销售合同</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("{sale_contract_id}")]
        [ProducesResponseType(typeof(SaleContractDto), 200)]
        public IActionResult Get([FromRoute(Name = "sale_contract_id")]long saleContractId)
        {
            return StatusCode((int)HttpStatusCode.OK, this.saleContractService.Get(saleContractId));
        }
        #endregion

        #region 获取销售合同列表
        /// <summary>
        ///  获取销售合同列表
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <response code="200">获取销售合同列表</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("User/{user_id}/List")]
        [ProducesResponseType(typeof(IEnumerable<SaleContractDto>), 200)]
        public IActionResult GetList([FromRoute(Name ="user_id")]long userId)
        {
            return StatusCode((int)HttpStatusCode.OK, this.saleContractService.GetList(userId));
        }
        #endregion

        #region 获取销售合同（分页）
        /// <summary>
        ///  获取销售合同（分页）
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="saleContractStatus">销售合同状态</param>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="sortName">排序字段</param>
        /// <param name="ascending">升序</param>
        /// <param name="index">当前页码</param>
        /// <param name="size">当前页大小</param>
        /// <response code="200">获取销售合同（分页）</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<SaleContractDto>), 200)]
        public IActionResult Query(
            [FromRoute(Name = "user_id")]long userId,
            [FromQuery(Name = "sale_contract_status")]SaleContractStatusType? saleContractStatus,
            [FromQuery(Name = "start")]DateTime? start,
            [FromQuery(Name = "end")]DateTime? end,
            [FromQuery(Name = "sort_name")]string sortName,
            [FromQuery(Name = "ascending")]bool? ascending,
            [FromQuery(Name = "index")]int index = 1,
            [FromQuery(Name = "size")]int size = 10)
        {
            return StatusCode((int)HttpStatusCode.OK, this.saleContractService.Query(userId, saleContractStatus,start, end, sortName, ascending, index, size));
        }
        #endregion
    }
}

